*========================================================*
* TABLE 1. Madness and Violent Purges (Baseline Models)  *
*========================================================*

* Panel settings
xtset id year

* Key independent variable: centered madness score (lag1)
gen mad_c = crazyscore_l1 - r(mean)

* Duration dependence: years since last purge (based on t-1)
bysort id (year): gen last_purge_prev = .
bysort id (year): replace last_purge_prev = year[_n-1] if violent_purge[_n-1]==1
bysort id (year): replace last_purge_prev = last_purge_prev[_n-1] if missing(last_purge_prev)
gen yrs_since_prev = year - last_purge_prev
replace yrs_since_prev = . if missing(last_purge_prev)

* Spell-time categories (duration dummies)
gen spellcat = .
replace spellcat = 0 if missing(yrs_since_prev)
replace spellcat = 1 if yrs_since_prev==1
replace spellcat = 2 if yrs_since_prev==2
replace spellcat = 3 if inrange(yrs_since_prev,3,4)
replace spellcat = 4 if yrs_since_prev>=5

* Define common sample (based on full specification)
quietly xtlogit violent_purge mad_c dem gwf_persol ln_realgdp_l1 gdp_change_l1 transleader, re vce(cluster id)
gen byte t1sample = e(sample)

* Estimate models
eststo clear
eststo M1: xtlogit violent_purge mad_c if t1sample, re vce(cluster id)
eststo M2: xtlogit violent_purge mad_c dem gwf_persol ln_realgdp_l1 gdp_change_l1 transleader if t1sample, re vce(cluster id)
eststo M3: xtlogit violent_purge mad_c dem gwf_persol ln_realgdp_l1 gdp_change_l1 transleader ib3.spellcat if t1sample, re vce(cluster id)

* Export table
esttab M1 M2 M3, se star(* 0.10 ** 0.05 *** 0.01) compress label keep(mad_c dem gwf_persol ln_realgdp_l1 gdp_change_l1 transleader 1.spellcat 2.spellcat 4.spellcat _cons) coeflabels(mad_c "Madness (lag1), centered" dem "Democracy" gwf_persol "Personalist regime" ln_realgdp_l1 "Log real GDP (t-1)" gdp_change_l1 "Real GDP (t-1)" transleader "Leadership transition" 1.spellcat "1 year since purge" 2.spellcat "2 years since purge" 4.spellcat "5+ years since purge" _cons "Constant") title("Table 1. Madness and Purges (Baseline RE Logit Models)")

*========================================================*
* TABLE 2. Conditional Contexts: Military & Tenure       *
*========================================================*

* Define common sample using saturated model
quietly xtlogit violent_purge c.mad_c##i.milservice c.mad_c##c.yrs_in_ofc dem gwf_persol ln_realgdp_l1 gdp_change_l1 transleader, re vce(cluster id)
replace t2sample = e(sample)

eststo clear

* M4: Madness + Military (main effects)
eststo M4: xtlogit violent_purge c.mad_c i.milservice dem gwf_persol ln_realgdp_l1 gdp_change_l1 transleader if t2sample, re vce(cluster id)

* M5: Madness × Military
eststo M5: xtlogit violent_purge c.mad_c##i.milservice dem gwf_persol ln_realgdp_l1 gdp_change_l1 transleader if t2sample, re vce(cluster id)

* M6: Madness + Tenure (main effects)
eststo M6: xtlogit violent_purge c.mad_c c.yrs_in_ofc dem gwf_persol ln_realgdp_l1 gdp_change_l1 transleader if t2sample, re vce(cluster id)

* M7: Madness × Tenure
eststo M7: xtlogit violent_purge c.mad_c##c.yrs_in_ofc dem gwf_persol ln_realgdp_l1 gdp_change_l1 transleader if t2sample, re vce(cluster id)

* Export table
esttab M4 M5 M6 M7, se star(* 0.10 ** 0.05 *** 0.01) compress label keep(mad_c 1.milservice 1.milservice#c.mad_c yrs_in_ofc c.mad_c#c.yrs_in_ofc dem gwf_persol ln_realgdp_l1 gdp_change_l1 transleader _cons) order(mad_c 1.milservice 1.milservice#c.mad_c yrs_in_ofc c.mad_c#c.yrs_in_ofc dem gwf_persol ln_realgdp_l1 gdp_change_l1 transleader _cons) coeflabels(mad_c "Madness (lag1), centered" 1.milservice "Military background" 1.milservice#c.mad_c "Madness × Military" yrs_in_ofc "Tenure (years in office)" c.mad_c#c.yrs_in_ofc "Madness × Tenure" dem "Democracy" gwf_persol "Personalist regime" ln_realgdp_l1 "Log real GDP (t-1)" gdp_change_l1 "Real GDP (t-1)" transleader "Leadership transition" _cons "Constant") title("Table 2. Conditional Contexts: Military and Tenure (RE Logit, Common Sample)")


*==============================*
* TABLE 3. Robustness checks   *
*==============================*
eststo clear

* M8: Alternative DV (all purges)
eststo M8: xtlogit purge mad_c dem gwf_persol ln_realgdp_l1 gdp_change_l1 transleader ib3.spellcat if t1sample, re vce(cluster id)

* M9: Alternative DV (Political Purges)
eststo M9: xtlogit purge_sub7 mad_c dem gwf_persol ln_realgdp_l1 gdp_change_l1 transleader ib3.spellcat if t1sample, re vce(cluster id)

* M10: Alternative lag (lag 2) on main DV
eststo M10: xtlogit violent_purge L2.mad_c dem gwf_persol ln_realgdp_l1 gdp_change_l1 transleader ib3.spellcat if t1sample, re vce(cluster id)

* M11: Alternative estimator (FE logit)
eststo M11: xtlogit violent_purge mad_c dem gwf_persol ln_realgdp_l1 gdp_change_l1 transleader ib3.spellcat if t1sample, fe

* Export table
esttab M8 M9 M10 M11, se star(* 0.10 ** 0.05 *** 0.01) compress label order(mad_c L2.mad_c dem gwf_persol ln_realgdp_l1 gdp_change_l1 transleader *.spellcat _cons) coeflabels(mad_c "Madness (lag1), centered" L2.mad_c "Madness (lag2), centered" dem "Democracy" gwf_persol "Personalist regime" ln_realgdp_l1 "Log real GDP (t-1)" gdp_change_l1 "Real GDP (t-1)" transleader "Leadership transition" _cons "Constant") title("Table 3. Robustness Checks (Alternative DVs, Lags, and Estimator; Common Sample)")

